package com.woniu.product.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.ForOrderProductDTO;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.ResponseData;
import com.woniu.product.dto.ShoppingCartDto;
import com.woniu.product.pojo.ShoppingCart;
import com.woniu.product.pojo.ZyProduct;
import com.woniu.product.service.ShoppingCartService;
import com.woniu.product.mapper.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【shopping_cart】的数据库操作Service实现
* @createDate 2024-08-15 12:17:31
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public ResponseData<?> addShoppingCart(ZyProduct product) throws ParseException {
        ShoppingCart shoppingCart = new ShoppingCart();
        Cookie[] cookies = httpServletRequest.getCookies();
        String jwt=null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals("jwt")){
                    jwt=cookie.getValue();
                }
            }
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        ShoppingCart sc = lambdaQuery().eq(ShoppingCart::getUserId, userInfo.get("id").toString())
                .eq(ShoppingCart::getProductId, product.getId())
                .one();
        if(ObjectUtil.isEmpty(sc)){
            long id = IdUtil.getSnowflake().nextId();
            shoppingCart.setId(String.valueOf(id));
            shoppingCart.setBuyNum(1);
            shoppingCart.setBuyPrice(product.getPrice());
            shoppingCart.setTotalPrice(product.getPrice().multiply(new BigDecimal(shoppingCart.getBuyNum())));
            shoppingCart.setUserId(userInfo.get("id").toString());
            shoppingCart.setCreateTime(new Date());
            shoppingCart.setStatus(0);
            shoppingCart.setProductId(product.getId());
            save(shoppingCart);
            return new ResponseData<>().success(shoppingCart.getBuyNum());
        }else {
            sc.setBuyNum(sc.getBuyNum()+1);
            sc.setTotalPrice(sc.getBuyPrice().multiply(new BigDecimal(sc.getBuyNum())));
            updateById(sc);
            return new ResponseData<>().success(sc.getBuyNum());
        }

    }

    @Override
    public ResponseData<?> selectShoppingCartList() throws ParseException {
        Cookie[] cookies = httpServletRequest.getCookies();
        String jwt=null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals("jwt")){
                    jwt=cookie.getValue();
                }
            }
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        String userId = userInfo.get("id").toString();
        List<ShoppingCartDto> shoppingCart = shoppingCartMapper.selectShoppingCartList(userId);

        return new ResponseData<>().success(shoppingCart);
    }

    @Override
    public ResponseData updateCart(String userId, List<ForOrderProductDTO> products) {
        for (ForOrderProductDTO product : products) {
            lambdaUpdate()
                    .set(ShoppingCart::getStatus,1)
                    .eq(ShoppingCart::getProductId,product.getId())
                    .eq(ShoppingCart::getUserId,userId)
                    .update();
        }

        return new ResponseData().success();
    }
}




